//
//  WRYPictureView.m
//  BaiSiBuJie
//
//  Created by wry on 16/5/4.
//  Copyright © 2016年 WRY. All rights reserved.
//

#import "WRYPictureView.h"
#import "WRYThemeItem.h"
#import "WRYLoadImageManager.h"
#import "WRYSeeBigPicctureViewController.h"
#import <DALabeledCircularProgressView.h>

@interface WRYPictureView ()
@property (weak, nonatomic) IBOutlet UIImageView *pictureView;
@property (weak, nonatomic) IBOutlet UIButton *bigPictureBtn;
@property (weak, nonatomic) IBOutlet UIImageView *gifVIew;
@property (weak, nonatomic) IBOutlet DALabeledCircularProgressView *preogressView;





@end

@implementation WRYPictureView

-(void)awakeFromNib
{
    //下载进度条属性设置
    _preogressView.roundedCorners=10;
    _preogressView.progressLabel.textColor=[UIColor whiteColor];

}

#pragma mark - 点击看大图按钮
- (IBAction)lockBigPictureBtnClick:(UIButton *)sender {
    //modal
    WRYSeeBigPicctureViewController * seeBigPVC=[[WRYSeeBigPicctureViewController alloc]init];
    
    //没有控制器展示modal时,想到窗口的跟控制器
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:seeBigPVC animated:YES completion:nil];
}

-(void)setItem:(WRYThemeItem *)item
{
    [super setItem:item];
    // gif标识
    _gifVIew.hidden=!item.is_gif;
    // 查看大图按钮
    _bigPictureBtn.hidden=!item.isBigPicture;
    //大图
    if (item.isBigPicture) {
        
        // 设置UIImageView内容模式
        _pictureView.contentMode= UIViewContentModeTop ;
        // 裁剪
        _pictureView.clipsToBounds=YES;
    }else{
        _pictureView.contentMode=UIViewContentModeScaleToFill;
        _pictureView.clipsToBounds=NO;
        }

/*-----------------------------------------------*/

    // 如果是大图，让图片不要压缩的特别难看
    // 注意:cell一定要记得还原
    SDWebImageCompletionBlock block;
    
    [WRYLoadImageManager wry_setImageToImageView:_pictureView WithURL:item.image0 placeholderImage:nil progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        //网速很慢时expectedSize 没有值,只有expectedSize>0时才显示下载进度
        if (expectedSize<0 || receivedSize<0) return ;
        CGFloat progress=1.0* receivedSize /expectedSize;//注意是1.0,不能是1
        _preogressView .progress=progress;
        _preogressView.progressLabel.text=[NSString stringWithFormat:@"%.1f%%",progress*100];
        
    } completed:block];
/*-----------------------------------------------*/
     // 如果是大图，让图片案例缩放后裁剪图片
    block=^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    
        // 每次都重新生成一张新的图片
        // 只有大图才需要，生成一张新，拉伸好的图片
        if (item.isBigPicture) {
        
            CGFloat h=self.pictureView.width/item.width*item.height;

            //开启上下文
            UIGraphicsBeginImageContext(CGSizeMake(self.width, h));
            //画图
            [image drawInRect:CGRectMake(0, 0, self.width, h)];
            //获取图片
            image= UIGraphicsGetImageFromCurrentImageContext();
            //结束上下文
            UIGraphicsEndImageContext();
            //设置图片
            self.pictureView.image=image;
            //保存到缓存替换原来的图片
            // 保存到Disk，利用SDWebImage,把之前缓存图片替换调用
            [[SDImageCache sharedImageCache] storeImage:image forKey:item.image0];
        }
        
    };
 
}







@end
